package com.shop.dao.bclass;

import com.alibaba.fastjson.asm.MethodWriter;
import com.shop.dao.BaseDao;
import com.shop.pajo.BClass;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements IClassDao{

    @Override
    public List<BClass> query(Connection connection, String queryCode) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<BClass> bClassList = new ArrayList<BClass>();
        BClass bClass = null;
        if(connection != null){
            String sql = "select * from bookclass where bid = ?";
            Object[] params = {queryCode};

            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()){
                bClass = new BClass();
                bClass.setId(resultSet.getString("bid"));
                bClass.setbClass(resultSet.getString("class"));

                bClassList.add(bClass);
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return bClassList;
    }

    @Override
    public BClass queryByAll(Connection connection, String queryCode, String queryClass) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        BClass bClass = null;
        if(connection != null){
            String sql = "select * from bookclass where bid=? and class=?";
            Object[] params = {queryCode,queryClass};
            resultSet = BaseDao.execute(connection,preparedStatement,resultSet,sql,params);
            while (resultSet.next()){
                bClass = new BClass();
                bClass.setId(resultSet.getString("bid"));
                bClass.setbClass(resultSet.getString("class"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return bClass;
    }

    @Override
    public int add(Connection connection, BClass bClass) throws SQLException {
        PreparedStatement preparedStatement = null;
        int addNum = 0;
        if(connection != null){
            String sql = "insert into bookclass values(?,?)";
            Object[] params = {bClass.getId(),bClass.getbClass()};
            addNum = BaseDao.execute(connection,preparedStatement,sql,params);

            BaseDao.closeResource(null,preparedStatement,null);
        }
        return addNum;
    }

    @Override
    public int deleteClassById(Connection connection, String id, String bClass) throws SQLException {
        PreparedStatement preparedStatement = null;
        int deleteNum = 0;
        if(connection != null){
            String sql = "delete from bookclass where bid= ? and class = ?";
            Object[] params = {id,bClass};
            deleteNum = BaseDao.execute(connection,preparedStatement,sql,params);

            BaseDao.closeResource(null,preparedStatement,null);
        }
        return deleteNum;
    }
}
